package com.sunmouse.toolkit.fragment.home;


import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.storage.StorageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sunmouse.toollib.adapter.BasicRecyclerAdapter;
import com.sunmouse.toollib.constants.Interaction;
import com.sunmouse.toollib.utils.FileUtils;
import com.sunmouse.toollib.utils.LogUtils;
import com.sunmouse.toollib.utils.system.PermissionUtil;
import com.sunmouse.toolkit.R;
import com.sunmouse.toolkit.adapter.FunAdapter;
import com.sunmouse.toolkit.bean.PageBean;
import com.sunmouse.toolkit.fragment.BaseFragment;
import com.sunmouse.toolkit.service.ExceptionService;
import com.sunmouse.toolkit.unzip.ZipUtil;
import com.sunmouse.toolkit.util.PageUtil;

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends BaseFragment {

    @BindView(R.id.m_list)
    RecyclerView mList;
    Unbinder unbinder;
    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_list, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void init() {
        mData = new ArrayList<>();
        mAdapter = new FunAdapter(mData, getActivity());
        this.mList.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        this.mList.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new ItemClick());
        loadingData(1);
    }


    @Override
    public void loadingData(int page) {
        mData.add(new PageBean(PageUtil.Index.MENU,"menu"));
        mData.add(new PageBean(PageUtil.Index.OTHER,"other"));
        mData.add(new PageBean(PageUtil.Index.CUSTOM,"custom"));
        mData.add(new PageBean(PageUtil.Index.NAV,"nav"));
        mData.add(new PageBean(PageUtil.Index.TV,"tv"));
        mData.add(new PageBean(PageUtil.Index.WEB,"web"));
        mData.add(new PageBean(PageUtil.Index.CHART,"chart"));
        mData.add(new PageBean(PageUtil.Index.ANIM,"anim"));
        mData.add(new PageBean(PageUtil.Index.DIALOG,"dialog"));
        mData.add(new PageBean(PageUtil.Index.FTP,"ftp"));
        mData.add(new PageBean(PageUtil.Index.CHAT,"chat"));
        mData.add(new PageBean(PageUtil.Index.SERVICE,"service"));
        mData.add(new PageBean(PageUtil.Index.FASHION,"fashion"));
        mData.add(new PageBean(PageUtil.Index.CONTROL,"control"));
        mData.add(new PageBean(PageUtil.Index.MATERIAL,"material"));
        mData.add(new PageBean(PageUtil.Index.CAMERA,"camera"));
        mData.add(new PageBean(PageUtil.Index.BLUETOOTH,"bluetooth"));

//        PageBean bean=mData.get(3);
//        bean.setTitle("I come back again");

        mAdapter.notifyDataSetChanged();
    }

    /**
     * launch service
     */
    private void launchService() {
        mIntent = new Intent(getActivity(), ExceptionService.class);
        getActivity().startService(mIntent);
    }

    /**
     *
     */
    private void checkUpdate() {
        if (!PermissionUtil.isPermitted(getActivity(), Interaction.Permissions.FILE_ACCESS)) {
            PermissionUtil.requestPermission(getActivity(), Interaction.Permissions.FILE_ACCESS, 5);
            return;
        }
        loadingUpdate();
    }

    /**
     * loading update
     */
    private void loadingUpdate() {
        checkFilePath();
        try {
            InputStream is = getActivity().getAssets().open("html/setIp.html");
            BufferedInputStream bis = new BufferedInputStream(is);
            OutputStream os = new FileOutputStream(FileUtils.getStoragePath() + "/resola/setIp.html");
            byte[] b = new byte[1024];
            int len;
            while ((len = bis.read(b, 0, b.length)) != -1) {
                os.write(b, 0, len);
                os.flush();
            }
            os.close();
            bis.close();

            ZipUtil.unzip(getActivity(), "reesola.zip", FileUtils.getStoragePath() + "/resola");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void checkFilePath() {
        StorageManager mManager = (StorageManager) getActivity().getSystemService(Context.STORAGE_SERVICE);
        try {
            Class<?>[] params = {};
            Method volumePath = StorageManager.class.getMethod("getVolumePaths", params);
            volumePath.setAccessible(true);
            Object[] p = {};
            String[] outPaths = (String[]) volumePath.invoke(mManager, p);
            for (String s : outPaths) {
                LogUtils.d("--------------" + s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 5 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            loadingUpdate();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    /**
     * item click
     */
    class ItemClick implements BasicRecyclerAdapter.OnItemClickListener{

        @Override
        public void onItemClick(int position) {
            int index=mData.get(position).getId();
            jump(index);

        }
    }



}
